package com.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.exception.ConsumerException;
import com.admin.mapper.AdChannelMapper;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.admin.service.AdChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdChannelImpl extends ServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService{

    @Autowired
    private AdChannelMapper am;

    /**
     *@Methods: findAll
     *@Param: [channelDto]
     *@Return: com.usian.model.common.dtos.PageResponseResult
     *@Author: 秉性哥
     *@CreatDate: 2022/10/8 18:42
     */
    @Override
    public PageResponseResult findAll(ChannelDto channelDto) {
        //创建模糊查询条件
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(channelDto.getName()!=null&&channelDto.getName()!=""?true:false,AdChannel::getName,channelDto.getName());
        //新建分页查询条件
        Page<AdChannel> page = new Page<>(channelDto.getPage(), channelDto.getSize());
        am.selectPage(page,wrapper);
        //创建返回对象
        PageResponseResult result = new PageResponseResult();
        //为返回对象进行赋值
        result.setData(page.getRecords());
        result.setTotal(page.getTotal());
        result.setCode(200);
        return result;
    }

    /**
     *@Methods: add
     *@Param: [adChannel]
     *@Return: com.usian.model.common.dtos.ResponseResult
     *@Author: 秉性哥
     *@CreatDate: 2022/10/8 18:42
     */
    @Override
    public ResponseResult add(AdChannel adChannel) {
        if (adChannel.getName()==""){
            throw new ConsumerException(5000,"姓名不能为空");
        }
        am.insert(adChannel);
        return new ResponseResult(200,"成功!");
    }

    /**
     *@Methods: edit
     *@Param: [adChannel]
     *@Return: com.usian.model.common.dtos.ResponseResult
     *@Author: 秉性哥
     *@CreatDate: 2022/10/8 18:42
     */
    @Override
    public ResponseResult edit(AdChannel adChannel) {
        if (adChannel.getName()==""){
            throw new ConsumerException(200,"姓名不能为空");
        }
        am.updateById(adChannel);
        return new ResponseResult(200,"成功!");
    }

    /**
     *@Methods: del
     *@Param: [id]
     *@Return: com.usian.model.common.dtos.ResponseResult
     *@Author:秉性哥
     *@CreatDate: 2022/10/8 18:42
     */
    @Override
    public ResponseResult del(Integer id) {
        am.deleteById(id);
        return new ResponseResult(200,"成功!");
    }

    /**
     *@Methods: findById
     *@Param: [id]
     *@Return: com.usian.model.common.dtos.ResponseResult
     *@Author: 秉性哥
     *@CreatDate: 2022/10/8 18:42
     */
    @Override
    public ResponseResult findById(Integer id) {
        AdChannel byId = am.selectById(id);
        return new ResponseResult(200,byId);
    }

    @Override
    public ResponseResult find() {
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdChannel::getStatus,1);
        lambdaQueryWrapper.orderByDesc(AdChannel::getOrd);
        List<AdChannel> list =  this.list(lambdaQueryWrapper);
        return ResponseResult.okResult(list);
    }
}
